package com.lace.waf.exception.entity;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.text.SimpleDateFormat;

/**
 * 服务内部接口响应类
 * @author joyin
 *
 */
public class WebResponseInfo extends BaseResponseInfo{
	private static final ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
	private static final Logger logger = LoggerFactory.getLogger(WebResponseInfo.class);

	private boolean success;
	private Object content;
	private String url;
	private long timestamp;
	private String code;
	private String msg;
	
	public WebResponseInfo() {}
	
	public WebResponseInfo(boolean success, Object content, String url) {
		this.content = content;
		this.url = url;
		this.success = success;
	}

	/**
	 * 取得指定对象的返回信息
	 * @param flag 标记是基础类型，还是对象，还是数组，：1=基础类型，2=对象，3=数组
	 * @return
	 */
	@SuppressWarnings({ "unchecked"})
	public <T> T getMessage(int flag, Class<?> collectionClass, Class<?>... elementClasses) {
		try {
			if(content == null) return null;
			mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
			switch(flag){
			case 1 :
				//基础类型
				return (T)content;
			case 2:
				//对象类型
				JSONObject obj = JSONObject.fromObject(content);
				return (T)mapper.readValue(obj.toString(), this.getJavaType(collectionClass, elementClasses));
			case 3:
				//数组类型
				JSONArray arrObj = JSONArray.fromObject(content);
				return mapper.readValue(arrObj.toString(), this.getJavaType(collectionClass, elementClasses));
			}
			return null;
		} catch (JsonParseException e) {
			logger.error("json转换异常:", e);
		} catch (JsonMappingException e) {
			logger.error("json转换异常:", e);
		} catch (IOException e) {
			logger.error("json转换异常:", e);
		}
		return null;
	}

	public Object getContent() {
		return content;
	}

	public void setContent(Object content) {
		this.content = content;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public static ObjectMapper getMapper() {
		return mapper;
	}

	public static Logger getLogger() {
		return logger;
	}
	
	public long getTimestamp() {
		return timestamp;
	}
	
	public void setTimestamp(long timestamp) {
		this.timestamp = timestamp;
	}
	
	public String getCode() {
		return code;
	}
	
	public void setCode(String code) {
		this.code = code;
	}
	
	public String getMsg() {
		return msg;
	}
	
	public void setMsg(String msg) {
		this.msg = msg;
	}

	/**
	 * 获取泛型的Type
	 * 作者：陈嘉瑛
	 * 时间：2015-04-11
	 * @param collectionClass 泛型的Collection
	 * @param elementClasses 元素类
	 * @return JavaType Java类型
	 * @since 1.0
	 */
	public JavaType getJavaType(Class<?> collectionClass, Class<?>... elementClasses) {
		return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
	}
}
